//
//  RaidersListVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/24.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "RaidersListVC.h"
#import "RaidersTableCell.h"
#import "RaiderDetailVC.h"
@interface RaidersListVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)NSArray * dataSource;

//@property (nonatomic)NSInteger pageno;
@end

@implementation RaidersListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setup];
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self.tableView.mj_header beginRefreshing];
}

- (void)setup {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20,kNavigationBarHeight, kScreenWidth - 40, kScreenHeight - kNavigationBarHeight - kBottomHeight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = (kScreenWidth - 40) * 9/16 + 55;
    self.tableView.sectionFooterHeight = 0.01;
    
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"RaidersTableCell" bundle:nil] forCellReuseIdentifier:@"RaidersTableCell"];
    self.title = MYLocalizedString(@"HOMEPAGE_STRATEGY", nil);
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *headView = [UIView new];
    headView.frame = CGRectMake(0, 0, kScreenWidth, 8);
    headView.backgroundColor = [UIColor clearColor];
    
    self.tableView.tableHeaderView = headView;
    
    [self closeAutomaticallyAdjustsScrollViewInsets:self.tableView];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        //            _pageno = 0;
        [self loadDataSource];
        //            [self.tableView.mj_footer resetNoMoreData];
    }];
    
    //    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    //        @strongify(self);
    //        [self loadDataSource];
    //    }];
    self.view.backgroundColor = kBGColor;
    [self.tableView setThemeStyle:ThemeStyleBlack];
    
}

- (void)loadDataSource {
    
    @weakify(self);
    
    [NetAPIManager getRaidersBy:self.currentCityID start:@(0) count:@(1000) block:^(NSArray *raidersArray, NSError *error)  {
        
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.dataSource = raidersArray;
        [self.tableView.mj_header endRefreshing];
        
        //          if (self.pageno == 0) {
        //
        //
        //              if (self.dataSource.count < 3) {
        //
        //                  self.tableView.mj_footer.hidden = YES;
        //              } else {
        //                  self.tableView.mj_footer.hidden = NO;
        //              }
        //          }else {
        //              [self.dataSource addObjectsFromArray:raidersArray];
        //              [self.tableView.mj_footer endRefreshing];
        //          }
        //
        //          if (raidersArray.count < 9) {
        //              [self.tableView.mj_footer endRefreshingWithNoMoreData];
        //          }
        
        [self.tableView reloadData];
        //          self.pageno ++;
        
    }];
    
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RaidersTableCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RaidersTableCell"];
    
    Raiders * model = self.dataSource[indexPath.section];
    
    [cell configData:model];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = [tableView  dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    
    if (!headerView) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
        headerView.backgroundColor = [UIColor clearColor];
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0.01;
    }
    
    return 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RaiderDetailVC * detailVC = [[RaiderDetailVC alloc] init];
    
    Raiders * raider = self.dataSource[indexPath.section];
    raider.seetimes = @(raider.seetimes.integerValue + 1).stringValue;
    detailVC.raider = raider;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@end

